const app = getApp();
import {
  formatTimeQiandao
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_Register: true,
    progress: 0,
    is_qiandao: "点此签到",
    levelDescripe: [
      "点点新宠",
      "见习帖主",
      "专业帖主",
      "银牌帖主",
      "银牌讲师",
      "银牌教授",
      "金牌讲师",
      "金牌专家",
      "超级专家",
      "认证专家"
    ]
  },
  onShow: function() {
    var that = this;
    that.getUserMsg();
  },

  //跳转至我的个人信息页面
  toPersonMsg: function() {
    var that = this;
    wx.navigateTo({
      url: '/pages/my/personalMsg?user_id=' + that.data.user_id,
    })
  },
  //教务绑定
  toZhjw: function(e) {
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      wx.navigateTo({
        url: '/pages/zhjw/bindzhjw',
      })
    }
  },
  toScore: function(e) {
    var that = this;
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      if (that.data.username == 0) {
        wx.showModal({
          title: '提示',
          content: '您尚未绑定教务系统',
          cancelText: '取消',
          confirmText: '去绑定',
          success: res => {
            if (res.confirm) {
              wx.navigateTo({
                url: "/pages/zhjw/bindzhjw"
              })
            }
          }
        })
      } else {
        wx.navigateTo({
          url: '/pages/zhjw/score',
        })
      }

    }
  },
  toClassTable: function(e) {
    var that = this;
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      if (that.data.username == 0) {
        wx.showModal({
          title: '提示',
          content: '未进行教务认证',
          cancelText: '取消',
          confirmText: '认证',
          success: res => {
            if (res.confirm) {
              wx.navigateTo({
                url: "/pages/zhjw/bindzhjw"
              })
            }
          }
        })
      } else {
        wx.switchTab({
          url: '/pages/classable/classable'
        })
      }
    }
  },


  //得到天气
  getWeather: function(res) {
    var that = this;
    wx.request({
      url: app.globalData.targetUrl + "search/weather/",
      method: "GET",
      success: function(res) {
        console.log(res);
        var weather = res.data.data.data.forecast;
        var ji = 2;
        for (var i = 0; i < weather.length; i++) {
          while (weather[i].high[ji] != " ") {
            ji++;
          }
          weather[i].high = weather[i].high.slice(ji);
          weather[i].low = weather[i].low.slice(ji);
          ji = 2;
        }
        var today = weather[0];
        var imagetype = "sunny";
        var str = today.type;
        if (today.type == "晴") {
          imagetype = "sunny";
        }
        if (today.type == "多云") {
          imagetype = "yin";
        }
        if (str.indexOf("雨") != -1) {
          imagetype = "yu";
        }
        if (str.indexOf("雨") != -1) {
          imagetype = "xue";
        }
        that.setData({
          weather: weather,
          today: today,
          imagetype: imagetype
        })
      }
    })
  },

  onToQinggui: function() {
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      wx.navigateTo({
        url: '/pages/my/qinggui',
      })
    }
  },
  //得到用户信息
  getUserMsg: function() {
    var that = this;
    var is_Register = wx.getStorageSync('is_Register');
    var user_id = wx.getStorageSync('user_id');
    that.setData({
      is_Register: is_Register,
      user_id: user_id
    })
    wx.request({
      url: app.globalData.targetUrl + "users/" + user_id + '/',
      success: function(res) {
        var user = res.data.data;
        wx.setStorageSync('username', res.data.data.student_id);
        that.setData({
          username: res.data.data.student_id
        })
        var credit = res.data.data.credit;
        console.log(credit)
        var progress = (parseInt(credit) * 100) / (Math.pow(2, parseInt(user.level)) * 20);
        console.log(progress)
        console.log(res.data);
        var campus = "金石滩";
        if (res.data.data.is_campus_type == false) {
          campus = "开发区";
        }
        that.setData({
          user: user,
          campus: campus,
          progress: progress
        })

        wx.request({
          url: app.globalData.targetUrl + "users/" + that.data.user_id + "/credit",
          method: "GET",
          data: {
            is_check: 1
          },
          success: function(res) {
            var error = res.data.error;
            var is_qiandao = "点此签到";
            if (error == 0) {
              is_qiandao = "今日已签"
            }
            that.setData({
              is_qiandao: is_qiandao
            })
          }
        })
      }
    })
  },

  //跳转页面“设置”
  tosettings: function() {
    var that = this;
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      wx.navigateTo({
        url: '/pages/my/mySetting?user_id=' + that.data.user_id,
      })
    }
  },
  //跳转页面“我的发布”
  toPublishHistory: function() {
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      wx.navigateTo({
        url: '/pages/history/publishhistory',
      })
    }

  },
  //跳转页面“我的浏览”
  toVisitHistory: function() {
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      wx.navigateTo({
        url: '/pages/history/visithistory',
      })
    }

  },
  //天气预报
  toWeather: function() {
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      wx.navigateTo({
        url: '/pages/my/tianqi'
      })
    }
  },
  //跳转页面“我的收藏”
  onToMyCollections: function() {
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      wx.navigateTo({
        url: '/pages/my/mycollections',
      })
    }

  },
  //跳转页面“我的帖子”
  onToMyTopics: function() {
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      wx.navigateTo({
        url: '/pages/my/myTopics',
      })
    }

  },
  //跳转页面“我的消息”
  toMyChatList: function() {
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      wx.switchTab({
        url: '/pages/chatlist/chatList',
      })
    }

  },
  //跳转页面“点点影评”
  onToMovie: function() {
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      wx.navigateTo({
        url: '/pages/movie/index',
      })
    }
  },

  //跳转页面"点点换肤"
  onToChangeSkin: function() {
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      wx.navigateTo({
        url: '/pages/my/changeSkin'
      })
    }
  },

  //跳转页面"关于点点"
  onToChangeDianDian: function() {
    wx.navigateTo({
      url: "/pages/aboutdiandian/showdiandian"
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function() {
    this.getWeather();
  },


  //弹出授权框
  bindGetUserInfo: function(e) {
    var that = this;
    var time = 3;
    that.setData({
      disabled: true
    })
    var Interval = setInterval(function() {
      time--;
      if (time > 0) {
        that.setData({
          disabled: true
        })
      } else {
        clearInterval(Interval);
        that.setData({
          disabled: false
        })
      }
    }, 1000)
    console.log(e.detail.userInfo);
    wx.setStorageSync('userInfo', e.detail.userInfo);
    if (e.detail.userInfo) {
      wx.navigateTo({
        url: '/pages/my/signup',
      })
    }
  },

  //校园网
  toNetwork: function() {
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      wx.navigateTo({
        url: '/pages/network/login'
      })
    }
  },

  //签到
  qiandao: function() {
    var that = this;
    var is_Register = wx.getStorageSync('is_Register');
    if (!is_Register) {
      wx.showModal({
        title: '提示',
        content: '请先注册',
        showCancel: false,
      })
    } else {
      var user_id = wx.getStorageSync('user_id');
      var check_time = formatTimeQiandao(new Date());
      console.log(check_time)
      wx.request({
        url: app.globalData.targetUrl + "users/" + user_id + "/credit/",
        method: "POST",
        data: {
          cnt: 5,
          check_time: check_time
        },
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function(res) {
          console.log(res.data);
          if (res.data.error == -9) {
            that.setData({
              is_qiandao: "今日已签"
            })
            wx.showModal({
              title: '今日已签',
              content: '明天再来吧',
              showCancel: false
            })
          } else {
            wx.showModal({
              title: '签到成功',
              content: '积分+5',
              showCancel: false
            })
            wx.request({
              url: app.globalData.targetUrl + "users/" + user_id + '/',
              success: function(res) {
                console.log(res.data);
                var user = res.data.data;
                var credit = res.data.data.credit;
                var progress = (parseInt(credit) * 100) / (Math.pow(2, parseInt(user.level)) * 20);
                console.log(progress);
                var campus = "金石滩";
                if (res.data.data.is_campus_type == false) {
                  campus = "开发区";
                }
                that.setData({
                  user: user,
                  progress: progress,
                  campus: campus,
                  is_qiandao: '今日已签'
                })
              }
            })
          }
        }
      })
    }
  }
})